<?php
/**
 * 实用类
 *
 * @author 孙晓晔
 * @version $Id: EmpUtil.class.php 1033 2010-03-25 11:26:55Z sunxy $
 */

require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class EmpUtil extends AdminUtil {
	public $sex = array(
		'-' => '请选择',
		'1' => '男',
		'2' => '女',	
	);
	
	public $source = array(
		'-' => '请选择',
		'厂门招聘' => '厂门招聘',
		'现场招聘' => '现场招聘',
		'网络招聘' => '网络招聘',
		'员工介绍' => '员工介绍',
		'员工返厂' => '员工返厂',
			
		'其它' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '在职',
		'2' => '暂停',
		'3' => '离职',

		'9' => '其它',
	);

	function __construct($request) {
		$request['update'] = '$Date: 2009/03/06 11:29:07 $';
		$request['revision'] = '$Revision: 1.2 $';
			
		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$this->t = array_merge($this->t, $this->b);
		
		$controller->assign_by_ref('sex_option', $this->sex);
		$controller->assign_by_ref('source_option', $this->source);
	}
	
	public $b = array(
		'car'=>'请输入车辆编号，然后选择并回车即可。',
		'driver'=>'请输入司机姓名拼音首字母，然后选择并回车即可。如张三，输入zs',
	);

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "id in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE " . $insql;
		$query = $db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}	

	function _edit_out($am, $row, $out) {
		if (!$am) {
			return false;
		}

		if ($row['out'] == $out) {
			return false;
		}

		$am->database_put('OUT', $row['exten'], $out);
	}
	
	function _t3($t2) {		
		if (strtotime($t2) < time()) {
			return 0;
		}
		
		return $this->date_diff('d', date("Y-m-d"), $t2);
	}
	
	function date_1($s1, $s2) {
		if (empty($s1) || empty($s2)) {
			return '';
		}
		
		$t1 = strtotime($s1);
		$t2 = strtotime($s2);
		
		$t = $t2 - $t1;
		
		if ($t < 0) {
			return '';
		}
		
		$h = 60 * 60;
		$d = $h * 24;
		$m = $d * 30;
		$y = $d * 365;
		
		$ry = 0;
		if ($t > $y) {
			$ry = floor($t / $y);			
			$t = $t % $y;
		}
		
		$rm = 0;
		if ($t > $m) {
			$rm = floor($t / $m);			
			$t = $t % $m;
		}
		
		$rd = 0;
		if ($t > $d) {
			$rd = floor($t / $d);			
			$t = $t % $d;
		}
		
		$fmt = "%02d年%02d月%02d日";
		$ret = sprintf($fmt, $ry, $rm, $rd);
		
		return $ret;
	}

	function date_diff($part, $begin, $end) {
		if (empty($begin) || empty($end)) {
			return '';
		}
		
		$diff = strtotime($end) - strtotime($begin);
		
		$ret = 0;
		switch ($part) {
			case "y": 
				$a = 60 * 60 * 24 * 365; 
				break;
			case "m": 
				$a = 60 * 60 * 24 * 30; 
				break;
			case "w": 
				$a = 60 * 60 * 24 * 7; 
				break;
			case "d": 
				$a = 60 * 60 * 24; 
				break;
			case "h": 
				$a = 60 * 60; 
				break;
			case "n": 
				$a = 60; 
				break;
			case "s": 
				$ret = $diff; 
				break;
		}
		
		if ($part == '' || $part == 's') {
			return $diff;
		}
		
		$ret = $diff / $a; 

		return $ret;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];
	
		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);
	
			return $row;
		}
	
		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
	
		$controller->assign('orderby', $orderby);
	
		return $row;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'12', '2'=>'13', '3'=>'14', '9'=>'15');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
	
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}
		
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}
	
	function _my_add() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'12', '2'=>'13', '3'=>'14');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
	
			return sprintf(" visible = 1 AND (office in (%s) OR oper = '%s')", $office, $emp);
		}
		
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}
	
	function _my_emp($controller, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $id);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);
			
			$ret[] = $row;
		}

		$controller->assign_by_ref('my', $ret);
	}
	
	function _update_emp($row) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s' status = '%s', modifytime = now() WHERE emp = '%s' ";
		$sql = sprintf($fmt, $this->tbl, $row['office'], $row['dept'], $row['status'], $row['id']);
		// debug($sql);
		$db->query($sql);
	}
}
?>